[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
.. The range operator, which is really two different
operators depending on the context. In an array
context, returns an array of values counting (by
ones) from the left value to the right value. This
is useful for writing "for (1..10)" loops and for
doing slice operations on arrays.
In a scalar context, .. returns a boolean value.
The operator is bistable, like a flip-flop.. Each
.. operator maintains its own boolean state. It is
false as long as its left operand is false. Once
the left operand is true, the range operator stays
true until the right operand is true, AFTER which
the range operator becomes false again. (It doesn't
become false till the next time the range operator
is evaluated. It can become false on the same
evaluation it became true, but it still returns true
once.) The right operand is not evaluated while the
operator is in the "false" state, and the left
operand is not evaluated while the operator is in
the "true" state. The scalar .. operator is pri-
marily intended for doing line number ranges after
the fashion of sed or awk. The precedence is a lit-
tle lower than || and &&. The value returned is
either the null string for false, or a sequence
number (beginning with 1) for true. The sequence
number is reset for each range encountered. The
final sequence number in a range has the string 'E0'
appended to it, which doesn't affect its numeric
value, but gives you something to search for if you
want to exclude the endpoint. You can exclude the
beginning point by waiting for the sequence number
to be greater than 1. If either operand of scalar
.. is static, that operand is implicitly compared to
the $. variable, the current line number. Examples:
As a scalar operator:
if (101 .. 200) { print; } # print 2nd hundred lines
next line if (1 .. /^$/); # skip header lines
s/^/> / if (/^$/ .. eof()); # quote body
As an array operator:
for (101 .. 200) { print; } # print $_ 100 times
@foo = @foo[$[ .. $#foo]; # an expensive no-op
@foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson